<template>
    <div class="account_login" v-loading="loading">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item prop="email">
                <el-input placeholder="请输入账号(邮箱号)" v-model="ruleForm.email" autocomplete="off">
                    <i slot="prefix" class="el-input__icon el-icon-user"></i>
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" placeholder="请输入密码" v-model.trim="ruleForm.password" autocomplete="off" show-password>
                    <i slot="prefix" class="el-input__icon el-icon-lock"></i>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" style="width: 100%;" @click="submitForm('ruleForm')" :disabled="!ruleForm.email || !ruleForm.password">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import loginApi from '../../api/apiUser';
export default {
    name: "Account_Login",
    data() {
        var checkemail = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('账号不能为空'));
            }
            let reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
            if(!reg.test(value)){
                return callback(new Error('账号格式有误！'));
            }else{
                callback()
            }
        };
        var checkpassword = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('密码不能为空'));
            }
            let reg = /^\w{5,7}$/
            if(!reg.test(value)){
                return callback(new Error('密码格式有误，请输入6-8位数字字母组合！'));
            }else{
                callback()
            }
        };
        return {
            ruleForm: {
                email:"",
                password:""
            },
            rules: {
                email: [
                    { validator: checkemail, trigger:  ['blur', 'change'] }
                ],
                password: [
                    { validator: checkpassword, trigger:  ['blur', 'change'] }
                ]
            },
            loading:false
        }
    },
    methods: {
        submitForm(name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    // this.loading = true
                    this.login()
                } else {
                    return false;
                }
            });
        },
        async login(){
           const result =  await loginApi.loging(this.ruleForm)
           if(result.code != 200) return this.$message.error(result.msg)
           localStorage.setItem("token",result.data.token)
           localStorage.setItem("userInfo",JSON.stringify(result.data))
           this.$message.success('登录成功')
           this.$router.push('/')
        }
    }
}
</script>
<style lang="less" scoped>
.account_login {
    width: 90%;
    height: 100%;
    margin: 0 auto;

    .demo-ruleForm {
        padding-top: 30px;
    }
}

/deep/ .el-form-item__content {
    margin-left: 0 !important;
}
</style>